﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace praca_v2
{
    public partial class Rejestracja : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void Zarejestruj(string dbTableName, string connectionString = "ConnectionString")
        {
            ConnectionStringSettings settings = System.Configuration.ConfigurationManager.ConnectionStrings[connectionString];
            SqlConnection connection = new SqlConnection(settings.ConnectionString);
            connection.Open();
            
            SqlCommand command = new SqlCommand("INSERT INTO " + dbTableName + " (Email, Haslo, Imie, Nazwisko, Numer_telefonu) " + "VALUES(@Email, @Haslo, @Imie, @Nazwisko, @Numer_telefonu)", connection);
            command.Parameters.AddWithValue("@Email", this.TextBoxEmail.Text);
            command.Parameters.AddWithValue("@Haslo", this.TextBoxHaslo.Text);
            command.Parameters.AddWithValue("@Imie", this.TextBoxImie.Text);
            command.Parameters.AddWithValue("@Nazwisko", this.TextBoxNazwisko.Text);
            command.Parameters.AddWithValue("@Numer_telefonu", this.TextBoxNumer.Text);

                command.ExecuteNonQuery();
            
           
             connection.Close(); 
        }
        protected void ButtonZarejestruj_Click(object sender, EventArgs e)
        {
            if (sprawdzEmail("Klienci"))
            {
                Zarejestruj("Klienci");
            }
            else
            {
                Response.Write("<script>alert('Podany adres jest juz w uzyciu')</script>");

            }
        }
        private bool sprawdzEmail(string dbTableName, string connectionString = "ConnectionString")
        {
            ConnectionStringSettings settings = System.Configuration.ConfigurationManager.ConnectionStrings[connectionString];
            SqlConnection connection = new SqlConnection(settings.ConnectionString);
            connection.Open();

            //SqlCommand liczbaWierszy = new SqlCommand("SELECT count(*) as ilosc FROM " + dbTableName, connection);
            //int iloscWierszy = Convert.ToInt32(liczbaWierszy.ExecuteScalar());
            //string[] tabEmail = new string[iloscWierszy];

            SqlCommand command = new SqlCommand("SELECT * FROM " + dbTableName, connection);
            SqlDataReader dr = command.ExecuteReader();
            bool sprEmail = true;
            while (dr.Read())
            {
                if(TextBoxEmail.Text.Trim() == dr["Email"].ToString().Trim())
                {
                    sprEmail = false;
                    
                }
                
            }
            connection.Close();
            return sprEmail;
        }
    
    }
}